package arraylist;

import ObjectList.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayList3 {
    public static void main(String[] args) {
        ArrayList<Student>studentArrayList=new ArrayList<>();
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入数据的个数:");
        int count=scanner.nextInt();
        arrayListMethod(studentArrayList,count);
      //  arrayMethod(count);
        ArrayList <Student>selectArrList=getArray(studentArrayList);
        for (int i = 0; i <selectArrList.size() ; i++) {
            System.out.println(selectArrList.get(i).getName() +selectArrList.get(i).getAge());
        }

    }
    private static ArrayList <Student>getArray(ArrayList<Student>arrayList){
        ArrayList <Student>selectArrList=new ArrayList<>();
        for (int i = 0; i <arrayList.size() ; i++) {
            if (arrayList.get(i).getAge()<18){
                selectArrList.add(arrayList.get(i));
            }
        }
        return selectArrList;
    }
    private static void arrayListMethod( ArrayList<Student>studentArrayList,int count) {
        Scanner scanner=new Scanner(System.in);
        for (int i = 0; i <count ; i++) {
            System.out.println("请输入姓名:");
            String name=scanner.next();
            System.out.println("请输入年龄:");
            int age=scanner.nextInt();
            Student stu=new Student(name,age);
            studentArrayList.add(stu);
        }

        for (int i = 0; i <studentArrayList.size() ; i++) {
            Student stu=studentArrayList.get(i);
            String name=stu.getName();
            int age=stu.getAge();
            System.out.println(name+age);
        }
    }

    private static void arrayMethod(int count) {
        Scanner scanner=new Scanner(System.in);
        Student []student=new Student[count];
        for (int i = 0; i <count ; i++) {
            System.out.println("请输入姓名:");
            String name=scanner.next();
            System.out.println("请输入年龄:");
            int age=scanner.nextInt();
            Student stu=new Student(name,age);
            student[i]=stu;
        }
        for (int i = 0; i <student.length ; i++) {
            System.out.println(student[i].getName()+student[i].getAge());
        }
    }
}
